﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace Alti.SharePoint.SPDataGenerator.Models
{
    public static class Converters
    {
        public static void GetListFieldWithUniquePascalLabel(List<SPDataField> Fields)
        {
            foreach (SPDataField aSPDataField in Fields)
            {
                if (Fields.Where(z => z.PascalLabel == aSPDataField.PascalLabel).Count() != 1)
                {
                    //Nous avons un doublon
                    if (Fields.Where(z => z.PascalLabel == aSPDataField.InternalName.NormalizeAndPascaliseString()).Count() == 0)
                        aSPDataField.PascalLabel = aSPDataField.InternalName.NormalizeAndPascaliseString();
                    else
                        aSPDataField.PascalLabel = aSPDataField.InternalName;
                }
            }
        }


        public static SPDataList SPListToSPDataList(SPList list)
        {
            SPDataList retSPDataList = new Models.SPDataList();

            //On supprime la page qui sert de vue
            if (list.DefaultViewUrl.LastIndexOf('/') >= 0)
                retSPDataList.ListUrl = list.DefaultViewUrl.Substring(0, list.DefaultViewUrl.LastIndexOf('/'));
            else
                retSPDataList.ListUrl = list.DefaultViewUrl;

            //On supprime les slash de début et de fin
            
            if(retSPDataList.ListUrl.EndsWith("/") && retSPDataList.ListUrl.Length > 0)
                retSPDataList.ListUrl = retSPDataList.ListUrl.Substring(0, retSPDataList.ListUrl.Length - 1);
            
            //S'il s'agit d'une doclib, la vue est dans le dossier Forms. Il faut le supprimer.
            if (retSPDataList.ListUrl.EndsWith("/Forms") && !retSPDataList.ListUrl.StartsWith("/Lists"))
                retSPDataList.ListUrl = retSPDataList.ListUrl.Substring(0, retSPDataList.ListUrl.IndexOf("/Forms"));


            retSPDataList.ListID = list.ID.ToString();
            retSPDataList.ListName = list.Title;
            retSPDataList.Hidden = list.Hidden;
            retSPDataList.Summary = list.Description;

            retSPDataList.Fields = new List<SPDataField>();
            foreach (SPField field in list.Fields)
            {
                retSPDataList.Fields.Add(SPFieldToSPDataField(field));
            }

            GetListFieldWithUniquePascalLabel(retSPDataList.Fields);

            retSPDataList.ContentTypes = new List<SPDataContentType>();
            foreach (SPContentType ct in list.ContentTypes)
                retSPDataList.ContentTypes.Add(SPContentTypeToSPDataContentType(ct));

            return retSPDataList;
        }


        public static SPDataContentType SPContentTypeToSPDataContentType(SPContentType ct)
        {
            SPDataContentType retSPDataContentType = new Models.SPDataContentType();
            retSPDataContentType.Name = ct.Name;
            retSPDataContentType.ID = ct.Id.ToString();
            retSPDataContentType.Hidden = ct.Hidden;
            retSPDataContentType.Summary = ct.Description;

            retSPDataContentType.Fields = new List<SPDataField>();
            foreach (SPField field in ct.Fields)
            {
                retSPDataContentType.Fields.Add(SPFieldToSPDataField(field));
            }

            GetListFieldWithUniquePascalLabel(retSPDataContentType.Fields);

            return retSPDataContentType;
        }


        public static SPDataField SPFieldToSPDataField(SPField field)
        {
            SPDataField retSPDataField = new Models.SPDataField();

            retSPDataField.InternalName = field.InternalName;
            retSPDataField.DisplayName = field.Title;
            retSPDataField.Hidden = field.Hidden;
            retSPDataField.Summary = field.Description;

            if (!String.IsNullOrEmpty(field.Title))
                retSPDataField.PascalLabel = field.Title.NormalizeAndPascaliseString();
            else
                retSPDataField.PascalLabel = field.InternalName.NormalizeAndPascaliseString();

            if (field.Type == SPFieldType.Choice)
            {
                SPFieldChoice choiceField = (field as SPFieldChoice);

                foreach (string aValue in choiceField.Choices)
                {
                    if(retSPDataField.FieldValues.Where(z => z.Key == aValue.NormalizeAndPascaliseString()).Count() > 0)
                        retSPDataField.FieldValues.Add(new KeyValuePair<string, string>(aValue.NormalizeAndPascaliseString() + (retSPDataField.FieldValues.Count + 1), aValue));
                    else
                        retSPDataField.FieldValues.Add(new KeyValuePair<string, string>(aValue.NormalizeAndPascaliseString(), aValue));
                }
            }

            return retSPDataField;
        }
    }
}
